package com.wxp.demo05.dialog;

import android.annotation.SuppressLint;
import android.app.TimePickerDialog;
import android.icu.util.Calendar;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.wxp.demo05.R;

/**
 * 4.3 时间对话框TimePickerDialog
 */
public class TimePickerDialogActivity extends AppCompatActivity implements View.OnClickListener, TimePickerDialog.OnTimeSetListener {

    private TextView tv_text;
    private TextView tv_text1;
    private TextView tv_text2;
    private TimePicker tp_time1;
    private TimePicker tp_time2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_time_picker_dialog);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        this.setTitle("4.3 时间对话框TimePickerDialog");

        this.findViewById(R.id.btn_ok).setOnClickListener(this);
        this.findViewById(R.id.btn_ok1).setOnClickListener(this);
        this.findViewById(R.id.btn_ok2).setOnClickListener(this);

        tv_text = this.findViewById(R.id.tv_text);
        tv_text1 = this.findViewById(R.id.tv_text1);
        tv_text2 = this.findViewById(R.id.tv_text2);

        tp_time1 = this.findViewById(R.id.tp_time1);
        tp_time2 = this.findViewById(R.id.tp_time2);
        //设置24小时制
        tp_time1.setIs24HourView(true);
        tp_time2.setIs24HourView(true);
    }

    @SuppressLint({"DefaultLocale", "SetTextI18n"})
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_ok) {
            //获取当前时间
            Calendar calendar = Calendar.getInstance();
            int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
            int minute = calendar.get(Calendar.MINUTE);
            //是否24小时制，true: 24小时制度，false：12小时制度
            boolean is24HourView = true;
            //主题样式
            /**
             * Theme_DeviceDefault_Light_Dialog: 时钟样式
             * Theme_Holo_Light_Dialog：下拉样式
             */
            int themeResId = android.R.style.Theme_Holo_Light_Dialog;
            //弹出时间对话框
            TimePickerDialog dialog = new TimePickerDialog(this, themeResId, this, hourOfDay, minute, is24HourView);
            // 显示时间对话框
            dialog.show();

        } else if (v.getId() == R.id.btn_ok1) {
            String text = tv_text1.getText().toString();
            int lastIndexOf = text.lastIndexOf("您选择");
            if (lastIndexOf > -1) {
                text = text.substring(0, lastIndexOf);
            }
            tv_text1.setText(text + String.format("您选择的时间是%d时%d分",
                    tp_time1.getHour(),
                    tp_time1.getMinute()
            ));
        } else if (v.getId() == R.id.btn_ok2) {
            String text = tv_text2.getText().toString();
            int lastIndexOf = text.lastIndexOf("您选择");
            if (lastIndexOf > -1) {
                text = text.substring(0, lastIndexOf);
            }
            tv_text2.setText(text + String.format("您选择的时间是%d时%d分",
                    tp_time2.getHour(),
                    tp_time2.getMinute()
            ));
        }
    }

    @SuppressLint({"DefaultLocale", "SetTextI18n"})
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        String text = tv_text.getText().toString();
        int lastIndexOf = text.lastIndexOf("您选择");
        if (lastIndexOf > -1) {
            text = text.substring(0, lastIndexOf);
        }
        tv_text.setText(text + String.format("您选择的时间是%d时%d分", hourOfDay, minute));
    }
}